package com.loongtech.bi.action.pay;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.pay.OverviewPayManager;
import com.loongtech.bi.manager.pay.OverviewPayManager.OverviewPayData;
import com.loongtech.bi.manager.pay.OverviewPayManager.PayClassData;
import com.loongtech.bi.manager.pay.OverviewPayManager.PayCompareData;
import com.loongtech.bi.manager.pay.OverviewPayManager.PayRangeData;
import com.loongtech.bi.manager.pay.OverviewPayManager.PayTrendData;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;

/**
 * 付费分析 —付费收入Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class OverviewPayAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	public final static String platformCompareTab = "platform";  // 付费对比标签页-平台
	public final static String channelCompareTab = "channel";  // 付费对比标签页-渠道
	public final static String serverCompareTab = "server";  // 付费对比标签页-警服
	public String compareTab = platformCompareTab;  // 当前选中的付费对比标签页，默认是平台
	
	private OverviewPayData overviewPayData;  // 前端付费收入总体数据 
	private List<PayTrendData> payTrendData;  // 前端付费趋势数据
	private ChartUIData payTrendChart;  // 前端付费趋势图表
	
	private List<PayClassData> payClassData;  // 付费分类
	private List<ChartUIData> payClassChart;  
	
	private List<PayRangeData> payRangeData;  // 前端付费区间数据
	private ChartUIData payRangeChart;  // 前端付费区间图表
	private List<PayCompareData> payCompareData;  // 前端付费对比数据
	private ChartUIData payCompareChart;  // 前端付费对比图表
	
	@Autowired
	private OverviewPayManager overviewPayManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "付费分析 —付费收入");
		
		payTrendChart = null;
		payClassChart = null;
		payCompareChart = null;
		
		overviewPayData = overviewPayManager.getOverviewPayData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		payTrendData = overviewPayManager.getPayTrendData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		if (payTrendData != null && payTrendData.size() > 0) {
			payTrendChart = new ChartUIData();
			payTrendChart.setJsonType(ChartType.AREA2.type);
			payTrendChart.setJsonStr(new ChartTool(payTrendData, PayTrendData.class, "").loadData(ChartType.AREA.type));
			payTrendChart.setAllItmes(ChartTool.getAllItems(PayTrendData.class));
		}
		
		// 付费分类
		payClassData = overviewPayManager.getPayClassData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		if (payClassData != null && payClassData.size() > 0) {
			payClassChart = new ArrayList<>();
			
			ChartUIData payClassChart0 = new ChartUIData();
			payClassChart0.setJsId("9988");
			payClassChart0.setJsonType(ChartType.PIE.type);
			payClassChart0.setJsonStr(new ChartTool(payClassData, PayClassData.class, "activeUser").loadData(ChartType.PIE.type));
			
			ChartUIData payClassChart1 = new ChartUIData();
			payClassChart1.setJsId("991");
			payClassChart1.setJsonType(ChartType.PIE.type);
			payClassChart1.setJsonStr(new ChartTool(payClassData, PayClassData.class, "payUser").loadData(ChartType.PIE.type));
			
			ChartUIData payClassChart2 = new ChartUIData();
			payClassChart1.setJsId("992");
			payClassChart2.setJsonType(ChartType.PIE.type);
			payClassChart2.setJsonStr(new ChartTool(payClassData, PayClassData.class, "payCount").loadData(ChartType.PIE.type));
			
			payClassChart.add(payClassChart0);
			payClassChart.add(payClassChart1);
			payClassChart.add(payClassChart2);
		}
		
		// 付费对比
		payCompareData = overviewPayManager.getPayCompareData(compareTab, dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		if (payCompareData != null && payCompareData.size() > 0) {
			payCompareChart = new ChartUIData();
			payCompareChart.setJsonType(ChartType.BAR.type);
			payCompareChart.setJsonStr(new ChartTool(payCompareData, PayCompareData.class, "").loadData(ChartType.BAR.type));
		}
		
		updatePayRangeData();
	}
	
	public void onTabChange(TabChangeEvent event) {
		if ("付费区间".equals(event.getTab().getTitle())) {
			updatePayRangeData();
		}
    }
	
	// 付费区间
	public void updatePayRangeData() {
		payRangeData = null;
		payRangeChart = null;
		
		FacesContext context = FacesContext.getCurrentInstance();
		if (HadoopQueryManager.VISIT_QUEUE.offer("OverviewPayAction")) {
			System.out.println("VISIT_QUEUE offer OverviewPayAction ok!");
			payRangeData = overviewPayManager.getPayRangeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
			if (payRangeData != null && payRangeData.size() > 0) {
				payRangeChart = new ChartUIData();
				payRangeChart.setJsonType(ChartType.BAR.type);
				payRangeChart.setJsonStr(new ChartTool(payRangeData, PayRangeData.class, "").loadData(ChartType.BAR.type));
			}
		} else {
			System.out.println("VISIT_QUEUE offer OverviewPayAction ok!");
			context.addMessage(null, new FacesMessage("提示", "系统繁忙，请稍后再试!"));
		}
	}
	
	// 响应前端曲线图改变选择项事件
	public void handleDateChange() throws Exception {
		payTrendChart.setJsonStr(new ChartTool(payTrendData, PayTrendData.class, payTrendChart.getSelectItem()).loadData(payTrendChart.getJsonType()));
	}
	
	// 切换付费对比标签页
	public void onCompareTabChange(TabChangeEvent event) {
		String eventTagId = event.getTab().getId();
		
		if (eventTagId != null && eventTagId.startsWith(channelCompareTab)) {
			compareTab = channelCompareTab;
		} else if (eventTagId != null && eventTagId.startsWith(serverCompareTab)) {
			compareTab = serverCompareTab;
		} else {
			compareTab = platformCompareTab;
		}
	
		payCompareData = overviewPayManager.getPayCompareData(compareTab, dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		if (payCompareData != null && payCompareData.size() > 0) {
			payCompareChart = new ChartUIData();
			payCompareChart.setJsonType(ChartType.BAR.type);
			payCompareChart.setJsonStr(new ChartTool(payCompareData, PayCompareData.class, "").loadData(ChartType.BAR.type));
		}
    }
         
	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public OverviewPayData getOverviewPayData() {
		return overviewPayData;
	}

	public void setOverviewPayData(OverviewPayData overviewPayData) {
		this.overviewPayData = overviewPayData;
	}

	public List<PayTrendData> getPayTrendData() {
		return payTrendData;
	}

	public void setPayTrendData(List<PayTrendData> payTrendData) {
		this.payTrendData = payTrendData;
	}

	public ChartUIData getPayTrendChart() {
		return payTrendChart;
	}

	public void setPayTrendChart(ChartUIData payTrendChart) {
		this.payTrendChart = payTrendChart;
	}

	public List<PayRangeData> getPayRangeData() {
		return payRangeData;
	}

	public void setPayRangeData(List<PayRangeData> payRangeData) {
		this.payRangeData = payRangeData;
	}

	public ChartUIData getPayRangeChart() {
		return payRangeChart;
	}

	public void setPayRangeChart(ChartUIData payRangeChart) {
		this.payRangeChart = payRangeChart;
	}

	public List<PayCompareData> getPayCompareData() {
		return payCompareData;
	}

	public void setPayCompareData(List<PayCompareData> payCompareData) {
		this.payCompareData = payCompareData;
	}

	public ChartUIData getPayCompareChart() {
		return payCompareChart;
	}

	public void setPayCompareChart(ChartUIData payCompareChart) {
		this.payCompareChart = payCompareChart;
	}

	public List<PayClassData> getPayClassData() {
		return payClassData;
	}

	public void setPayClassData(List<PayClassData> payClassData) {
		this.payClassData = payClassData;
	}

	public List<ChartUIData> getPayClassChart() {
		return payClassChart;
	}

	public void setPayClassChart(List<ChartUIData> payClassChart) {
		this.payClassChart = payClassChart;
	}
}
